#!/usr/bin/env python
#blast_0.8.py
#includes url fetching the contigs

import cgi, sys, re, os
from Bio.Blast import NCBIStandalone 
#from Bio.Blast import NCBIXML
from html import *
from Bio import SeqIO

idRawstr = r'><a name = ([\w\d\_]{1,15})></a>'
idCompiled=re.compile(idRawstr)
        
def blastAll():
    blastInput = open("/tmp/blast.input", "w")
    blastInput.write(str(sequence))
    blastInput.close()    
    
    blastcmd='/usr/bin/blastall'
    blastDbPath = '/usr/lib/cgi-bin/opuntia/'
    
    #by default:
    database = blastDbPath + 'opuntiaAnnotated'
    
    #that's for selecting the database to blast against
    if db == 'opuntia':
        database = '"' + blastDbPath + 'opuntiaAnnotated ' + \
            blastDbPath + 'singlets' + '"'
           
    elif db == 'contigs':
        database = blastDbPath + 'opuntiaAnnotated'
    
    elif db == 'singlets':
        database = blastDbPath + 'singlets'
     
    elif db == 'Mcrystallinum':
        database = blastDbPath + 'Mcrystallinum'
        
    elif db == 'Wmirabilis':
        database = blastDbPath + 'Wmirabilis'
      
    elif db == 'Acomosus':
        database = blastDbPath + 'Acomosus'
        
    elif db == 'OWMA':
        database = '"' + blastDbPath + 'opuntiaAnnotated' + ' ' + \
            blastDbPath + 'singlets' + ' ' + \
            blastDbPath + 'Wmirabilis' + ' ' + \
            blastDbPath + 'Mcrystallinum' + ' ' + \
            blastDbPath + 'Acomosus' + ' ' + '"'
            
    elif db == 'others':
        database= blastDbPath + 'others'
        
    elif db == 'whole':
        database = '"' + blastDbPath + 'opuntiaAnnotated' + ' ' + \
            blastDbPath + 'singlets' + ' ' + \
            blastDbPath + 'Wmirabilis' + ' ' + \
            blastDbPath + 'Mcrystallinum' + ' ' + \
            blastDbPath + 'Acomosus' + ' ' + \
            blastDbPath + 'others' + '"'
                
    os.environ['BLASTDB'] = blastDbPath + database
    
    blast_out, error_info = NCBIStandalone.blastall(blastcmd=blastcmd,
                                                    program=program,
                                                    database=database,
                                                    infile='/tmp/blast.input',
                                                    expectation=expect,
                                                    descriptions=descriptions,
                                                    alignments=alignments,
                                                    #outfile='/tmp/blastOut.txt',
                                                    #seqalign_file='/tmp/blastOut2.txt',
                                                    html='T',
                                                    align_view=alignmentView,
                                                    matrix = matrix        
                                                    )
    
    print error_info.read()

    #this is the classical pairwise view
    if alignmentView == "0":
        print "here we are!!!!!!!"
        i= 0
        #headings removal
        while i <3:
            line = blast_out.readline()
            i+=1
        
        #the idea is to add the url allowing to fetch the obtained sequences
        while line:
            newstr = re.sub(idCompiled, r'<a name = \1 href ="http://srvgen.upct.es/cgi-bin/opuntia/fetch.cgi?identifiers=\1">', line)
                
            print newstr.rstrip()
            line = blast_out.readline()
    
    else:
        print "<pre>"
        print blast_out.read()
        print "</pre>"
   
#form get
form = cgi.FieldStorage()
program = form.getvalue('PROGRAM') 
expect = form.getvalue('EXPECT') 
sequence = form.getvalue('SEQUENCE') 
matrix = form.getvalue('MATRIX')
alignmentView = form.getvalue('ALIGNMENT_VIEW')
descriptions = form.getvalue('DESCRIPTIONS')
alignments = form.getvalue('ALIGNMENTS')
db = form.getvalue('DATABASE')

htmlUp() 
blastAll()
